package cn.wjdiankong.levideo.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.view.LayoutInflater;
import android.view.View;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.view.LevideoDialog;
import cn.wjdiankong.levideo.view.seekbar.DiscreteSeekBar;
import cn.wjdiankong.levideo.view.seekbar.DiscreteSeekBar.OnProgressChangeListener;

public class VideoSettingDialog {
	
	private LevideoDialog dialog;
    private float brightness;
    private int mMaxVolume;
    private AudioManager audioManager;
	private Activity act;
	private View parentView;
	
	@SuppressLint("InflateParams")
	public VideoSettingDialog(Activity act){
		this.act = act;
		audioManager = (AudioManager) act.getSystemService(Context.AUDIO_SERVICE);
        mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        
        if(parentView == null){
        	parentView = LayoutInflater.from(act).inflate(R.layout.dialog_video_setting, null);
        }
        if(dialog == null){
        	dialog = new LevideoDialog(act, parentView, UIUtil.dip2px(act, 300), 0);
        }
		final DiscreteSeekBar lightBar = (DiscreteSeekBar) parentView.findViewById(R.id.bright_video_seekBar);
		lightBar.setValueColor(0xFFFFFFFF);
		final DiscreteSeekBar voiceBar = (DiscreteSeekBar) parentView.findViewById(R.id.voice_video_seekBar);
		voiceBar.setValueColor(0xFFFFFFFF);
		lightBar.setOnProgressChangeListener(new OnProgressChangeListener() {
			@Override
			public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
			}
			
			@Override
			public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
				setBrightness(value);
			}
		});
		voiceBar.setOnProgressChangeListener(new OnProgressChangeListener() {
			@Override
			public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
			}
			
			@Override
			public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(DiscreteSeekBar seekBar, int value, boolean fromUser) {
				setVoice(value);
			}
		});
		
		voiceBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) * 100 / mMaxVolume);
		lightBar.setProgress((int) (act.getWindow().getAttributes().screenBrightness * 100));
	}
	
	public void setBrightness(int value) {
        android.view.WindowManager.LayoutParams layout = act.getWindow().getAttributes();
        if (brightness < 0) {
            brightness = act.getWindow().getAttributes().screenBrightness;
            if (brightness <= 0.00f) {
                brightness = 0.50f;
            } else if (brightness < 0.01f) {
                brightness = 0.01f;
            }
        }
        if (value < 1) {
            value = 1;
        }
        if (value > 100) {
            value = 100;
        }
        layout.screenBrightness = 1.0F * (float) value / 100.0F;
        if (layout.screenBrightness > 1.0f) {
            layout.screenBrightness = 1.0f;
        } else if (layout.screenBrightness < 0.01f) {
            layout.screenBrightness = 0.01f;
        }
        act.getWindow().setAttributes(layout);
    }
	
	public void setVoice(int value){
		int index = (int) (mMaxVolume * value * 0.01);
        if (index > mMaxVolume)
            index = mMaxVolume;
        else if (index < 0)
            index = 0;
        // 变更声音
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
	}
	
	public void show(){
		if(dialog != null){
			dialog.show();
		}
	}

}
